﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConjureBivouac2012.GameStates;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ConjureBivouac2012;

namespace ConjureBivouac2012.Object
{
    public class AbstractVehicule : Entity
    {
        protected int health;
        protected int hurt;
        private const int maxDeathAnim = 18;
        protected AbstractShield shield;

        private Texture2D _healthBarTexture;
        private Vector2 healthPos = new Vector2(2,2);

        public override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();
            shield.Initialize();
            this.LoadContent(GameInfo.GetInstance().GetContent());
            myName = PLAYER;
        }

        public override void LoadContent(ContentManager contentManager)
        {
            sprite.LoadContent(contentManager, "Ship/ShipB");
            _healthBarTexture = contentManager.Load<Texture2D>("HealthBar");
            shield.LoadContent(contentManager);
            sprite.Depth = 0.1f;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
            shield.Draw(spriteBatch, sprite.Position, GetRadius());

            spriteBatch.Draw(_healthBarTexture, new Rectangle((int)healthPos.X, (int)healthPos.Y, health * 15, 8),
                new Rectangle(0, 45, _healthBarTexture.Width, 44), Color.Red, 0, new Vector2(0, 0), SpriteEffects.None, 0.1f);
        }

        public AbstractShield GetShield()
        {
            return shield;
        }

        public bool IsHurt(Vector2 colliderPos)
        {
            return shield.IsHurt(colliderPos);
        }

        public void SetRightThumbAngle(float angle)
        {
            shield.SetStartingAngle(angle);
        }

        public override void Update(GameTime time)
        {
            this.GetShield().Update(time);

            if(animationCount == 0)
            {
                if (hurt == 1)
                {
                    lastTime = time.TotalGameTime;
                    hurt = 2;
                }
                if (hurt == 2)
                {
                    if (time.TotalGameTime.TotalMilliseconds - lastTime.TotalMilliseconds > animationDelay)
                    {
                        hurt = 0;
                        sprite.LoadContent(GameInfo.GetInstance().GetContent(), "Ship/ShipB");
                    }
                }

                base.Update(time);
            }
            else
            {
                if (animationCount != maxDeathAnim)
                {
                    if (time.TotalGameTime.TotalMilliseconds - lastTime.TotalMilliseconds > animationDelay)
                    {
                        sprite.LoadContent(GameInfo.GetInstance().GetContent(), "ShipDied/ShipDied" + animationCount);
                        animationCount++;
                        lastTime = time.TotalGameTime;
                    }
                }
                else
                {
                    Program.GetGameInstance.SetState(new GameOver(GameInfo.GetInstance().GetContent(), GameInfo.GetInstance().GetDevice()));
                }
            }
        }

        public bool OnHurt(Vector2 colliderPos)
        {
            if (IsHurt(colliderPos - sprite.Position))
            {
                //Get damaged
                if (animationCount == 0 && hurt == 0)
                {
                    health--;
                    if (health != 0)
                    {
                        sprite.LoadContent(GameInfo.GetInstance().GetContent(), "ShipHit/ShipHit");
                        hurt = 1;
                    }
                    else
                    {
                        sprite.LoadContent(GameInfo.GetInstance().GetContent(), "ShipDied/ShipDied1");
                        animationCount = 1;
                        animationDelay = 150;
                    }

                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public void IncreaseHealth(int increase)
        {
            if (increase > 0)
            {
                health += increase;
            }
        }
    }
}
